package com.chuangjiangx.member.coupon.ddd.domain.repository;

import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.coupon.ddd.domain.model.GetChannelEnum;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrPayGiftCardRule;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrPayGiftCardRuleId;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrPayGiftCardWxStatusEnum;
import com.chuangjiangx.member.basic.dao.mapper.InMbrPayGiftCardRuleMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrPayGiftCardRule;
import com.chuangjiangx.member.basic.dao.model.InMbrPayGiftCardRuleExample;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wp
 * @date 2018-08-31 15:59
 **/
@Component
public class MbrPayGiftCardRuleRepository implements Repository<MbrPayGiftCardRule, MbrPayGiftCardRuleId> {

    private final InMbrPayGiftCardRuleMapper inMbrPayGiftCardRuleMapper;

    @Autowired
    public MbrPayGiftCardRuleRepository(InMbrPayGiftCardRuleMapper inMbrPayGiftCardRuleMapper) {
        this.inMbrPayGiftCardRuleMapper = inMbrPayGiftCardRuleMapper;
    }

    @Override
    public MbrPayGiftCardRule fromId(MbrPayGiftCardRuleId mbrPayGiftCardRuleId) {
        InMbrPayGiftCardRule inMbrPayGiftCardRule = inMbrPayGiftCardRuleMapper.selectByPrimaryKey(mbrPayGiftCardRuleId.getId());
        return convertToDomain(inMbrPayGiftCardRule);
    }

    public MbrPayGiftCardRule findByMerchantId(MerchantId merchantId) {
        InMbrPayGiftCardRuleExample example = new InMbrPayGiftCardRuleExample();
        example.createCriteria().andMerchantIdEqualTo(merchantId.getId());
        List<InMbrPayGiftCardRule> inMbrPayGiftCardRules = inMbrPayGiftCardRuleMapper.selectByExample(example);
        return inMbrPayGiftCardRules.isEmpty() ? null : wrap(inMbrPayGiftCardRules.get(0));
    }

    @Override
    public void update(MbrPayGiftCardRule mbrPayGiftCardRule) {
        InMbrPayGiftCardRule inMbrPayGiftCardRule = convertToIn(mbrPayGiftCardRule);
        inMbrPayGiftCardRuleMapper.updateByPrimaryKeySelective(inMbrPayGiftCardRule);
    }

    @Override
    public void save(MbrPayGiftCardRule mbrPayGiftCardRule) {
        InMbrPayGiftCardRule inMbrPayGiftCardRule = convertToIn(mbrPayGiftCardRule);
        int id = inMbrPayGiftCardRuleMapper.insertSelective(inMbrPayGiftCardRule);
        mbrPayGiftCardRule.setId(new MbrPayGiftCardRuleId(inMbrPayGiftCardRule.getId()));
    }

    private MbrPayGiftCardRule convertToDomain(InMbrPayGiftCardRule inMbrPayGiftCardRule) {
        MbrPayGiftCardRule mbrPayGiftCardRule = new MbrPayGiftCardRule(
                new MerchantId(inMbrPayGiftCardRule.getMerchantId()),
                inMbrPayGiftCardRule.getWxRuleId(),
                inMbrPayGiftCardRule.getBeginTime(),
                inMbrPayGiftCardRule.getEndTime(),
                inMbrPayGiftCardRule.getLeastCost(),
                inMbrPayGiftCardRule.getMaxCost(),
                GetChannelEnum.fromCodeList(inMbrPayGiftCardRule.getGetChannelList()),
                MbrPayGiftCardWxStatusEnum.fromCode(inMbrPayGiftCardRule.getWxStatus()),
                inMbrPayGiftCardRule.getCreateTime(),
                inMbrPayGiftCardRule.getUpdateTime()
        );
        mbrPayGiftCardRule.setId(new MbrPayGiftCardRuleId(inMbrPayGiftCardRule.getId()));
        return mbrPayGiftCardRule;
    }

    private InMbrPayGiftCardRule convertToIn(MbrPayGiftCardRule mbrPayGiftCardRule) {
        InMbrPayGiftCardRule inMbrPayGiftCardRule = new InMbrPayGiftCardRule();
        inMbrPayGiftCardRule.setId(mbrPayGiftCardRule.getId() == null ? null : mbrPayGiftCardRule.getId().getId());
        inMbrPayGiftCardRule.setMerchantId(mbrPayGiftCardRule.getMerchantId() == null ? null : mbrPayGiftCardRule.getMerchantId().getId());
        inMbrPayGiftCardRule.setBeginTime(mbrPayGiftCardRule.getBeginTime());
        inMbrPayGiftCardRule.setEndTime(mbrPayGiftCardRule.getEndTime());
        inMbrPayGiftCardRule.setLeastCost(mbrPayGiftCardRule.getLeastCost());
        inMbrPayGiftCardRule.setMaxCost(mbrPayGiftCardRule.getMaxCost());
        inMbrPayGiftCardRule.setWxRuleId(mbrPayGiftCardRule.getWxRuleId());
        inMbrPayGiftCardRule.setGetChannelList(mbrPayGiftCardRule.getGetChannelList() == null ? null : GetChannelEnum.toJsonList(mbrPayGiftCardRule.getGetChannelList()));
        inMbrPayGiftCardRule.setWxStatus(mbrPayGiftCardRule.getWxStatus() == null ? null : mbrPayGiftCardRule.getWxStatus().code);
        inMbrPayGiftCardRule.setCreateTime(mbrPayGiftCardRule.getCreateTime());
        inMbrPayGiftCardRule.setUpdateTime(mbrPayGiftCardRule.getUpdateTime());
        return inMbrPayGiftCardRule;
    }

    public MbrPayGiftCardRule wrap(InMbrPayGiftCardRule in) {

        MbrPayGiftCardRule m = new MbrPayGiftCardRule(
                new MerchantId(in.getMerchantId()),in.getWxRuleId(), in.getBeginTime(), in.getEndTime(), in.getLeastCost(), in.getMaxCost(), GetChannelEnum.fromCodeList(in.getGetChannelList()), MbrPayGiftCardWxStatusEnum.fromCode(in.getWxStatus()), in.getCreateTime(), in.getUpdateTime()
        );
        m.setId(new MbrPayGiftCardRuleId(in.getId()));
        return m;
    }

}
